package com.wenson.behavior.mediator;

import java.util.Objects;

/**
 * @ClassName: User
 * @Author: luowangcheng
 * @Date: 2024/6/16 9:34
 * @Description:
 */
public class User {
    private String name;
    private BaseChatRoom chatRoom;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void login(BaseChatRoom chatRoom) {
        this.chatRoom = chatRoom;
        this.chatRoom.register(this);
    }

    protected void logout() {
        chatRoom.unregister(this);
        this.chatRoom = null;
    }

    public void talk(User to, String msg) {
        if (Objects.isNull(chatRoom)) {
            System.out.println("【" + name + "的对话框】" + "您还没有登录");
            return;
        }
        chatRoom.sendMsg(this, to, msg);
    }

    public void listen(User from, User to, String msg) {
        System.out.print("【" + this.name + "的对话框】");
        System.out.println(chatRoom.processMsg(from, to, msg));
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || getClass() != obj.getClass()) return false;
        User user = (User) obj;
        return Objects.equals(name, user.name);
    }
}
